package com.lightsh.coverflow;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;

public class ImageUtility {
	/**
	 * 给图片添加倒影
	 * 原理:先翻转图片，由上到下放大透明度
	 */
	public static Bitmap createReflectedImage(Bitmap srcImage) {
		if(srcImage == null){
			return null;
		}
		int width = srcImage.getWidth();
		int height = srcImage.getHeight();
		
		// 原图片的底部与倒影间的垂直距离
		final int reflectionGap = 4;
		final int reflectHeight = height / 8;

		Matrix matrix = new Matrix();
		
		// 先进行翻转 操作
		matrix.preScale(1, -1);

		// 创建倒影的图片
		Bitmap reflectionImage = Bitmap.createBitmap(srcImage, 0, height - reflectHeight, width, reflectHeight, matrix, false);

		// Create a new bitmap with same width but taller to fit reflection
		// 创建一张图片，是目标图片的大小
		Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + reflectHeight + reflectionGap), Config.ARGB_8888);

		// 使用目标图片初始化canvas
		Canvas canvas = new Canvas(bitmapWithReflection);
		// 画原始图片
		canvas.drawBitmap(srcImage, 0, 0, null);
		// 画间隔
		Paint defaultPaint = new Paint();
		canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
		// 画倒影
		canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

		// 创建线性的变化
		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, 
				height, 0, bitmapWithReflection.getHeight(),
				0x70ffffff, 0x00ffffff, TileMode.CLAMP);
		paint.setShader(shader);
		
		// 设置画的模式(与操作?)
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		// Draw a rectangle using the paint with our linear gradient
		// 话
		canvas.drawRect(0, height + reflectionGap, width, bitmapWithReflection.getHeight(), paint);
		return bitmapWithReflection;
	}
	
	//drawable 类型转化为bitmap
	public static Bitmap drawableToBitmap(Drawable drawable) {
		if(drawable == null){
			return null;
		}
		Bitmap bitmap = Bitmap.createBitmap(
						drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(),
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
								: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		return bitmap;
	}
	
	public static Bitmap scallBitmap(Bitmap srcBitmap, float scallRange){
		if(srcBitmap == null){
			return null;
		}
		int width = srcBitmap.getWidth();
		int height = srcBitmap.getHeight();
		Matrix matrix = new Matrix();
		matrix.postScale(scallRange, scallRange);
		return Bitmap.createBitmap(srcBitmap, 0, 0, width, height, matrix, true);
	}
}
